<template>
  <div>
    <van-nav-bar title="黑马程序员-addgoods" left-text="返回" @click-left="onClickLeft" />
    <!-- 表单区域 -->
    <form action="/">
      <van-field v-model="value" placeholder="请输入商品名称" />
    </form>
    <van-button @click="addshanpin" class="btn" type="info">添加商品</van-button>
    <van-popup
      duration
      v-model="add"
      position="top"
      :style="{ height: '5%','color': '#fff' , 'background-color': 'greenyellow' ,'text-align': 'center'}"
    >已添加</van-popup>
  </div>
</template>

<script>
export default {
  data() {
    return {
      add: false,
      value: ''
    };
  },
  created: function() {},
  methods: {
    // 返回上一页
    onClickLeft() {
      this.$router.push('/search');
    },
    addshanpin: async function() {
      await this.$axios.post('http://www.liulongbin.top:3005/api/addproduct', {
        name: this.value
      });
      this.add = true;
      setTimeout(() => {
        this.add = false;
      }, 1500);
      setTimeout(() => {
        this.$router.push('/search');
      }, 3000);
    }
  }
};
</script>

<style scoped>
.btn {
  width: 100%;
  margin-top: 20px;
}
</style>
